package cn.tedu.submarine;
import java.util.Random;
import javax.swing.ImageIcon;
import java.awt.Graphics;
/** 海洋对象 */
public abstract class SeaObject {
    public static final int LIVE = 0;//活着的
    public static final int DEAD = 1;//死了的
    protected int state = LIVE;//当前状态，默认为活着的

    protected int width;//宽
    protected int height;//高
    protected int x;//x坐标
    protected int y;//y坐标
    protected int speed;//移动速度
    /**  专门给侦查潜艇，鱼雷潜艇，水雷潜艇准备的       */
    //因为三种潜艇的宽高是不同的，所以数据不能写死，需要传参写活
    //因为三种潜艇的x、y、speed是相同，所以直接写死，不需要传参
    public SeaObject(int width,int height) {
        this.width = 63;
        this.height = 19;
        x = -width;//负的潜艇宽
        Random rand = new Random();//
        y = rand.nextInt(World.HEIGHT - height - 150 + 1) + 150;
        speed = rand.nextInt(3) + 1;
    }
    public SeaObject(int width,int height,int x,int y,int speed){
       this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    /** 海洋对象移动     */
    public abstract void move();

    /**获取对像图片  */
    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state==LIVE;//若当前状态为活着，则返回true，否则返回false。
    }
    public boolean isDead(){
        return state==DEAD;//若当前状态为DEAD，则返回true，，否则返回true。
    }
    /**   画对象 g：画笔*/
    public void paintImage(Graphics g) {
        if (isLive()) {//若活着的
            this.getImage().paintIcon(null, g, this.x, this.y);//画对象
        }
    }
        public SeaObject nextThunder(){
            int x = this.x+this.width;
            int y = this.y-5;
            if (this instanceof TorpedoSubmarine){//若当前潜艇为鱼雷潜艇类型
                return new Torpedo(x,y);//
            }else if (this instanceof MineSubmarine){
                return new Mine(x,y);//
            }else {
                return null;//不发雷
            }
        }
        /**判断潜艇是否越界*/
        public boolean isOutOfBound(){
        return this.x>=World.WIDTH;
        }
        public boolean isHit(SeaObject other){
            //假设：this代指潜艇，other代指深水炸弹  调用：潜艇.isHit(深水炸弹)
            int x1 = this.x-other.width;
            int x2 = this.x+this.width;
            int y1 = this.y-other.height;
            int y2 = this.y+this.height;
            int x = other.x;//x：深水炸弹的x
            int y = other.y;//y：深水炸弹的y
            return x>=x1 && x<=x2
                    &&
                    y>=y1 && y<=y2;
        }
        public void goDead(){
            state = DEAD;//将对象的状态修改为DEAD死了的
        }
    }







































